﻿using System;
using System.Data;
using System.Data.SqlClient;
using DBUtility;
using System.Collections.Generic;
using System.Text;
using IDAL.RF.PowerTest;
using Model.RF.PowerTest;

namespace DAL.RF.PowerTest
{
    class PowerTest_temp : BaseRepository_DT<PowerTest_tempInfo>, IPowerTest_temp
    {
        #region 静态变量部分
        private const string PARM_POWERTEST_ID = "@ID";
        private const string PARM_POWERTEST_PRODUCTSN = "@ProductSN";
        private const string PARM_POWERTEST_PRODUCTTYPEID = "@ProductTypeID";
        private const string PARM_POWERTEST_COUPLERVALUE = "@CouplerValue";
        private const string PARM_POWERTEST_USERID = "@UserID";
        private const string PARM_POWERTEST_TESTSTEP = "@TestStep";
        private const string PARM_POWERTEST_PCIP = "@PCIP";
        private const string PARM_POWERTEST_SIGNALSOURCE = "@SignalSource";
        private const string PARM_POWERTEST_ISPASS = "@IsPass";
        private const string PARM_POWERTEST_TESTBAND = "@TestBand";
        private const string PARM_POWERTEST_FLAG = "@Flag";

        private const string PARM_POWERTEST_TESTANDITEMDT = "@PowerTestItemdt";
        #endregion

        #region 数据操作部分
        //-------------------------------------用于射频PIM测试数据录入的Web Service-----------------------------------------------------
        /// <summary>
        /// 测试记录与详细数据同时写入
        /// </summary>
        /// <param name="asi">测试记录信息</param>
        /// <param name="itemdt">测试详细数据表</param>
        /// <returns></returns>
        public bool PowerTest_tempAndItem_Insert(PowerTest_tempInfo ti, DataTable itemdt)
        {
            List<SqlParameter> ItemList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_POWERTEST_TESTANDITEMDT,itemdt)};
            SqlParameter[] Parms = GetParameters();
            SetParameters(ti, Parms);

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in ItemList)
                cmd.Parameters.Add(parm);

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                try
                {
                    cmd.CommandText = "PowerTestAndItem_temp_Insert";
                    using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { }
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public IList<PowerTest_tempInfo> PowerTest_temp_GetLatestbyProductSN(string productSN)
        {
            SqlParameter parm = new SqlParameter(PARM_POWERTEST_PRODUCTSN, SqlDbType.NVarChar, 50);
            parm.Value = productSN;
            return ExcuteSqlToGetEntities("PowerTest_temp_selectLatestbyProductSN", parm);
        }

        public void PowerTest_temp_DisableRecordsbySN(string productSN)
        {
            SqlParameter parm = new SqlParameter(PARM_POWERTEST_PRODUCTSN, SqlDbType.NVarChar, 50);
            parm.Value = productSN;
            sqlHelper.ExecuteNonQuery(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "PowerTest_temp_UpdateFlagbyProductSN", parm);
        }
        #endregion

        #region 重写基类部分
        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
                    new SqlParameter(PARM_POWERTEST_PRODUCTSN, SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_POWERTEST_PRODUCTTYPEID, SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_POWERTEST_COUPLERVALUE, SqlDbType.NVarChar,10),
                    new SqlParameter(PARM_POWERTEST_USERID, SqlDbType.NVarChar,10),
                    new SqlParameter(PARM_POWERTEST_TESTSTEP, SqlDbType.NVarChar,10),
                    new SqlParameter(PARM_POWERTEST_PCIP, SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_POWERTEST_SIGNALSOURCE, SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_POWERTEST_ISPASS, SqlDbType.Bit,1),
                    new SqlParameter(PARM_POWERTEST_TESTBAND, SqlDbType.NVarChar,20),
                    new SqlParameter(PARM_POWERTEST_FLAG, SqlDbType.Bit,1),
                };
            return parms;
        }

        public override void SetParameters(PowerTest_tempInfo Entity, params SqlParameter[] Parms)
        {
            Parms[0].Value = Entity.ProductSN;
            Parms[1].Value = Entity.ProductTypeID;
            Parms[2].Value = Entity.CouplerValue;
            Parms[3].Value = Entity.UserID;
            Parms[4].Value = Entity.TestStep;
            Parms[5].Value = Entity.PCIP;
            Parms[6].Value = Entity.SignalSource;
            Parms[7].Value = Entity.IsPass; 
            Parms[8].Value = Entity.TestBand;
            Parms[9].Value = Entity.Flag;
        }
        #endregion

        //-------------------------------------重写超类基础操作---------------------------------------------------------

        public override PowerTest_tempInfo GetEntityRecord(SqlDataReader rdr)
        {
            PowerTest_tempInfo pro = new PowerTest_tempInfo()
            {
                ID = rdr.GetInt32(0),
                ProductSN = rdr.GetValue(1).ToString(),
                RecordTime = rdr.GetDateTime(2),
                ProductTypeID = rdr.GetValue(3).ToString(),
                CouplerValue =rdr.GetValue(4).ToString(),
                UserID = rdr.GetValue(5).ToString(),
                TestStep = rdr.GetValue(6).ToString(),
                PCIP = rdr.GetValue(7).ToString(),
                SignalSource = rdr.GetValue(8).ToString(),
                IsPass = rdr.GetBoolean(9),
                TestBand = rdr.GetValue(10).ToString()
            };
            return pro;
        }

        public override PowerTest_tempInfo GetEntityRecord()
        {
            return new PowerTest_tempInfo();
        }
    }
}
